//
//  SourceEditor.m
//  iCodeMac
//
//  Created by Andrew Barry on 27/02/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "SourceEditor.h"
//#import "AppModule.h"
#import "MyTextViewDelegate.h"

@implementation SourceEditor

-(id) initWithModule: (AppModule *) module {
	self = [super init];

	ownerModule = [module retain];
	[NSBundle loadNibNamed:@"Editor" owner:self];
	return self;
}

-(void)show {
	[window makeKeyAndOrderFront:self];
	[window setDelegate:self];

}

-(NSString *) text {
	return [textview string];
}

-(void) setText: (NSString *) text {
	[textview setString: text];
}

-(void)windowWillClose:(NSNotification *)notification {
	[ownerModule sourceEditorClosing];
	window = nil;
}

-(void)debugRun:(id) sender {
	[[ownerModule stack] debugRun: sender];
}

-(void)debugStop: (id) sender {
	[[ownerModule stack] debugStop: sender];
}

- (void) errorSheetClose:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo {
}

-(void)runtimeErrorMessage: (NSString *) message withReport: (JSErrorReport *) report {
	NSString *str = [textview string];
	int lineStart = lineToPos(str, report->lineno);
	NSRange span = [str lineRangeForRange:NSMakeRange(lineStart, 0)];
	[textview setSelectedRange:span];
	
	NSAlert *alert = [[NSAlert alloc] init];
	[alert setMessageText:@"An error has occurred"];
	[alert setInformativeText:message];
	[alert addButtonWithTitle:@"Close"];
	[alert setAlertStyle:NSWarningAlertStyle];
	
	[alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(errorSheetClose:returnCode:contextInfo:) contextInfo:nil];

}
@end
